Shader "URP/Shadow"
{
    Properties
    {
        _BaseColor("Base Color",color) = (1,1,1,1)
        _BaseMap("BaseMap", 2D) = "white" {}
    }

    SubShader
    {
        Tags { "Queue"="Geometry" "RenderType" = "Opaque" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" }
        LOD 100

        Pass
        {
            Name "Unlit"
            HLSLPROGRAM
            // Required to compile gles 2.0 with standard srp library
            #pragma prefer_hlslcc gles
            #pragma exclude_renderers d3d11_9x
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fog
            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"

             //接收阴影 URP
            #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
            #pragma multi_compile _ _SHADOWS_SOFT

            struct Attributes
            {
            float4 positionOS       : POSITION;
                float2 uv               : TEXCOORD0;
            };

            struct Varyings
            {
                float4 positionCS       : SV_POSITION;
                float2 uv               : TEXCOORD0;
                float fogCoord          : TEXCOORD1;
                // 接收的阴影坐标 URP
                float4 shadowCoord : TEXCOORD4;
            };

            CBUFFER_START(UnityPerMaterial)
            half4 _BaseColor;
            float4 _BaseMap_ST;
            CBUFFER_END
            TEXTURE2D (_BaseMap);SAMPLER(sampler_BaseMap);


            Varyings vert(Attributes v)
            {
                Varyings o = (Varyings)0;

                o.positionCS = TransformObjectToHClip(v.positionOS.xyz);
                o.uv = TRANSFORM_TEX(v.uv, _BaseMap);
                o.fogCoord = ComputeFogFactor(o.positionCS.z);
                float3 positionWS = TransformObjectToWorld(v.positionOS.xyz);
                  // 通过世界坐标获取阴影坐标位置
                o.shadowCoord = TransformWorldToShadowCoord(positionWS);
                return o;
            }

            half4 frag(Varyings i) : SV_Target
            {
                half4 c;
                half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, i.uv);
                c = baseMap * _BaseColor;
                c.rgb = MixFog(c.rgb, i.fogCoord);

                 //lamber求模型表面的阴影面  _Step  再求出硬边明暗面
                Light mainLight = GetMainLight(i.shadowCoord);
                c *= mainLight.shadowAttenuation;

                return c;
            }
            ENDHLSL
        }

        // 生成阴影
        // Pass
        // {
        //     Tags{ "LightMode" = "ShadowCaster" }
		// 	HLSLPROGRAM
		// 	#pragma vertex vert
		// 	#pragma fragment frag
		// 	#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
 
		// 	struct appdata
		// 	{
		// 		float4 vertex : POSITION;
		// 	};
 
		// 	struct v2f
		// 	{
		// 		float4 pos : SV_POSITION;
		// 	};
 
		// 	sampler2D _MainTex;
		// 	float4 _MainTex_ST;
 
		// 	v2f vert(appdata v)
		// 	{
		// 		v2f o;
		// 		o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
		// 		return o;
		// 	}
		// 	float4 frag(v2f i) : SV_Target
		// 	{
		// 		float4 color;
		// 		color.xyz = float3(0.0, 0.0, 0.0);
		// 		return color;
		// 	}
		// 	ENDHLSL
        // }

        Pass
        {
            Name "ShadowCaster"
            Tags{"LightMode" = "ShadowCaster"}

            ZWrite On
            ZTest LEqual
            //Cull[_Cull]

            HLSLPROGRAM
            // Required to compile gles 2.0 with standard srp library
            // #pragma prefer_hlslcc gles
            // #pragma exclude_renderers d3d11_9x
            #pragma target 2.0

            // -------------------------------------


            //--------------------------------------
            // GPU Instancing
            #pragma multi_compile_instancing

            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/Shaders/SimpleLitInput.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/Shaders/ShadowCasterPass.hlsl"

            struct appdata
            {
                float4 positionOS   : POSITION;
                float3 normalOS     : NORMAL;
                float2 texcoord     : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float2 uv           : TEXCOORD0;
                float4 positionCS   : SV_POSITION;
            };


            v2f vert(appdata input)
            {
                v2f output;
                UNITY_SETUP_INSTANCE_ID(input);

                output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
                //output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
                // GetShadowPositionHClip 这个不加就会有摩尔纹(锯齿)
                output.positionCS = GetShadowPositionHClip(input);
                return output;
            }

            half4 frag(v2f input) : SV_TARGET
            {
                //Alpha(SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a, _BaseColor, _Cutoff);
                return 0;
            }

            ENDHLSL
        }

    }
}
